package com.ccc.boot.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ccc.boot.constant.DictConstant;
import com.ccc.boot.system.entity.RelRoleResource;
import com.ccc.boot.system.mapper.RelRoleResourceMapper;
import com.ccc.boot.system.service.RelRoleResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author superC
 * @version 0.1.0
 * @date 2022/4/2
 */
@Service
public class RelRoleResourceServiceImpl implements RelRoleResourceService {

    @Autowired
    private RelRoleResourceMapper relRoleResourceMapper;

    @Override
    public boolean insert(RelRoleResource entity) {
        return relRoleResourceMapper.insert(entity) > 0;
    }

    @Override
    public boolean insert(Long roleId, Integer resourceType, Long resourceId) {
        RelRoleResource entity = new RelRoleResource();
        entity.setRoleId(roleId);
        entity.setResourceType(resourceType);
        entity.setResourceId(resourceId);
        return relRoleResourceMapper.insert(entity) > 0;
    }

    @Override
    public boolean insertMenus(Long roleId, List<Long> menuIds) {
        return relRoleResourceMapper.insertBatch(roleId, DictConstant.RESOURCE_TYPE_API, menuIds) > 0;
    }

    @Override
    public boolean insertApis(Long roleId, List<Long> apiIds) {
        return relRoleResourceMapper.insertBatch(roleId, DictConstant.RESOURCE_TYPE_API, apiIds) > 0;
    }

    @Override
    public boolean delete(Integer resourceType, Long resourceId) {
        LambdaQueryWrapper<RelRoleResource> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RelRoleResource::getResourceType, resourceType);
        wrapper.eq(RelRoleResource::getResourceId, resourceId);
        if (check(wrapper)) {
            return relRoleResourceMapper.delete(wrapper) > 0;
        }
        return false;
    }

    @Override
    public void delete(Long roleId, Integer resourceType, Long resourceId) {
        LambdaQueryWrapper<RelRoleResource> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RelRoleResource::getRoleId, roleId);
        wrapper.eq(RelRoleResource::getResourceType, resourceType);
        wrapper.eq(RelRoleResource::getResourceId, resourceId);
        if (check(wrapper)) {
            relRoleResourceMapper.delete(wrapper);
        }
    }

    @Override
    public boolean deleteApis(List<Long> apiIds) {
        return relRoleResourceMapper.deleteBatch(null, DictConstant.RESOURCE_TYPE_API, apiIds) > 0 ;
    }

    @Override
    public boolean deleteApis(Long roleId, List<Long> apiIds) {
        return relRoleResourceMapper.deleteBatch(roleId, DictConstant.RESOURCE_TYPE_API, apiIds) > 0 ;
    }

    @Override
    public boolean deleteResource(Long roleId) {
        LambdaQueryWrapper<RelRoleResource> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RelRoleResource::getRoleId, roleId);
        relRoleResourceMapper.delete(wrapper);
        return false;
    }

    @Override
    public boolean check(Long roleId) {
        LambdaQueryWrapper<RelRoleResource> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RelRoleResource::getRoleId, roleId);
        return check(wrapper);
    }

    @Override
    public boolean check(Integer resourceType, Long resourceId) {
        LambdaQueryWrapper<RelRoleResource> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RelRoleResource::getResourceType, resourceType);
        wrapper.eq(RelRoleResource::getResourceId, resourceId);
        return check(wrapper);
    }

    @Override
    public boolean check(Long roleId, Integer resourceType, Long resourceId) {
        LambdaQueryWrapper<RelRoleResource> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RelRoleResource::getRoleId, roleId);
        wrapper.eq(RelRoleResource::getResourceType, resourceType);
        wrapper.eq(RelRoleResource::getResourceId, resourceId);
        return check(wrapper);
    }

    @Override
    public boolean check(LambdaQueryWrapper<RelRoleResource> wrapper) {
        return relRoleResourceMapper.selectCount(wrapper) > 0;
    }

    @Override
    public List<Long> getMenuIds(Long roleId) {
        return relRoleResourceMapper.getMenuIds(roleId, DictConstant.RESOURCE_TYPE_MENU);
    }

}